/**
 * 匿名函数测试
 * 《Javascript的一种模块模式》中的第一句话就是“全局变量是魔鬼”。
 */
//(function(){
//	var NAMESPACE = 'HW';
//	
//	window[ NAMESPACE ] = {
//		alert: function(){	alert('test');	}
//	};
//	
//	window['Haaway'] = {
//		tok:function(){console.debug('爱！')}
//	}
//})();


(function(){
	if(window.Haaway !== undefined) return;
	var HW = {};
	
	function gl1(a){
		console.debug('这是内容公用函数一'+a);
	}
	HW.test = function(a){
		gl1(a);
	}
	
	
	
	// tell global varible
	if(window.HW === undefined) window.HW = HW;
	window.Haaway = HW;
	
})();


(function($){
	//compulate position pixels
	function initPos(){
		var pTop = $(document).scrollTop()+100, pLeft = ($(document).width()-$('.hw-box').width())/2;
		return [pLeft,pTop ];
	}
	var JHW = {};
	JHW.ver = '0.1.1';//20110801
	JHW.options={
		title:'标题',
		content:'demo内容',
		bg: true,//是否显示背景遮照
		//show:'', //'' | 'slow' | 'fast'
		hide:'', //'' | 'slow' | 'fast'		
		onclose: null // callback func
	};
	JHW.html_hw_box_bg = '<div class="hw-box-bg"></div>';
	JHW.html_hw_box = '<div class="hw-box"><div class="hw-box-title"><span class="hw-box-title-content"></span><a class="hw-box-title-close" href="javascript:void(0)">X</a></div><div class="hw-box-content"></div></div>';
	JHW.alert = function(opts){
		var opts = $.extend(JHW.options, opts);
		//console.debug(opts);
		if(opts.bg){$('body').append(JHW.html_hw_box_bg);$('.hw-box-bg').css({width:$(document).width()});}
		
		$('body').append(JHW.html_hw_box);
		//compulate position pixels
		var _parr=initPos();
		//console.debug(_parr);
		$('.hw-box').css({left:_parr[0]+'px',top:_parr[1]+'px'});
		$('.hw-box-title-content').html( opts.title);
		$('.hw-box-content').html(opts.content );
		$('.hw-box-title-close').click(function(){
			$('.hw-box').hide(opts.hide); if($('.hw-box-bg')) $('.hw-box-bg').hide();
			if(opts.onclose){ opts.onclose('我了个去呀！msg');}
		})
		.focus();
	};
	
	/**
	 * 
	 * @param {String} msg
	 * @param {Object} callback
	 */
	JHW.alert_o = function(msg,callback,attr){
		var h='<div class="hw-box"></div>';
		if(attr.bg){ //背景
			var hbg='<div class="hw-box-bg"></div>';$('body').append(hbg);
			$('.hw-box-bg').css({width:$(document).width()});
		}
		$('body').append(h);		
		var pTop = $(document).scrollTop()+100, pLeft = ($(document).width()-$('.hw-box').width())/2;
		//console.debug(pLeft);
		msg+='窗口宽'+$(window).width()+',文档宽'+$(document).width() + '本框宽'+$('.hw-box').width()+'<br>'+pLeft;
		$('.hw-box').html(msg).css({top:pTop+'px', left:pLeft+'px'})
		.click(function(){$(this).hide('slow'); if(attr.bg){$('.hw-box-bg').hide()}});
		//if(callback) callback(str);
	}
	if(window.JHW === undefined) window.JHW = JHW;
	
})(jQuery);
